﻿using System;

namespace N2.Json.Converters
{
	using Newtonsoft.Json;

	/// <summary>
	/// Generate a JSON object stub for a server ContentItem objected intended to be loaded lazily.
	/// Stub format borrowed from ASP.Net Data Services implementation.
	/// </summary>
	class JsonContentItemProxyConverter : JsonConverter
	{
		public override bool CanConvert(Type objectType)
		{
			return typeof(ContentItem).IsAssignableFrom(objectType);
		}
		//TODO implement deserialization
		public override object ReadJson(JsonReader reader, Type objectType)
		{
			throw new NotImplementedException();
		}

		public override void WriteJson(JsonWriter writer, object value)
		{
			var _item = value as ContentItem;

			if (null == _item) {
				writer.WriteNull();
			}
			///<see cref="System.Data.Services.Serializers.JsonSerializer">WriteDeferredContentElement</see>
			writer.WriteStartObject();
			writer.WritePropertyName("__deferred");
			writer.WriteStartObject();
			//TODO should it be an absolute URL ?
			writer.WritePropertyName("uri");
			writer.WriteValue(_item.Url);
			writer.WriteEndObject();
			writer.WriteEndObject();
		}
	}
}
